﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System.Reflection;
namespace Client
{
    class StockMenuFuncs
    {
        public static void OpenTickView(DataCenter.Models.IInfo ggInfo)
        {
            try
            {
                if (!DataCenter.DataCenter.HavePermission(DataCenter.MyConst.APINames.TICK))
                {
                    AskTopUp();
                    return;
                }
                if (ggInfo != null)
                {
                    var p = new Panels.TickLinePanel(ggInfo);
                    Managers.Manager.panelM.AddPanel(p);
                }
            }
            catch (DataCenter.MyException.APINoPermissionException)
            {
                AskTopUp();
            }
        }

        public static void OpenKlineView(DataCenter.Models.IInfo ggInfo)
        {
            try
            {
                if (!DataCenter.DataCenter.HavePermission(DataCenter.MyConst.APINames.LASTK))
                {
                    AskTopUp();
                    return;
                }
                if (ggInfo != null)
                {
                    var p = new Panels.GGKLinePanel(ggInfo, "day");
                    Managers.Manager.panelM.AddPanel(p);
                }
            }
            catch (DataCenter.MyException.APINoPermissionException)
            {
                AskTopUp();
            }
        }

        public static void OpenNews(DataCenter.Models.IInfo info)
        {
            try
            {
                Managers.Manager.newsForm.ShowForm(info);
            }
            catch (DataCenter.MyException.APINoPermissionException)
            {
                AskTopUp();
            }
        }

        public static void OpenGeneralInfomation()
        {
            try
            {
                if (Managers.Manager.hotStockInfo != null)
                {
                    var t = GetStockGeneralInfomation(Managers.Manager.hotStockInfo.symbol);
                    var f = new Forms.InfomationForm(t, Managers.Manager.hotStockInfo.name + "基本信息");
                    f.ShowDialog();
                }
            }
            catch (DataCenter.MyException.NullValueException e)
            {
                MessageBox.Show("抱歉，暂无该股票基本信息。", "提示");
            }
            catch (DataCenter.MyException.APINoPermissionException)
            {
                AskTopUp();
            }
        }
        static string GetStockGeneralInfomation(string symbol)
        {
            try
            {
                var jo = DataCenter.DataCenter.GetStockGeneralInfomation(symbol);
                string text = "";
                for (int i = 0; i < jo["columns"].Count(); i++)
                {
                    string jargon = (string)jo["columns"][i];
                    if (jargon == "setup_date")
                    {
                        var timeStr = (string)jo["data"][i];
                        DateTime time;
                        DateTime.TryParseExact(timeStr, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out time);
                        text += Managers.Manager.jargonM.GetJargon(jargon) + "：" + time.ToString("yyyy年M月dd日") + "\r\n";
                        continue;
                    }
                    else
                        jargon = Managers.Manager.jargonM.GetJargon(jargon);
                    text += jargon + "：" + jo["data"][i] + "\r\n";
                }
                return text;
            }
            catch (DataCenter.MyException.APIReturnException e)
            {
                var l = e.Message.Split('~');
                if (l[0] == "1000")
                    Utility.ShowLoginAgain();
                throw;
            }
        }

        public static void ChoiceKTime(DataCenter.Models.IInfo gginfo)
        {
            if (!DataCenter.DataCenter.HavePermission(DataCenter.MyConst.APINames.GETK))
            {
                AskTopUp();
                return;
            }
            if (gginfo != null)
                Managers.Manager.selectKRangeForm.Open(Managers.Manager.hotStockInfo);
        }

        public static void UpdateHotStockInfo(DataCenter.Models.StockInfo stockInfo)
        {
            if (Managers.Manager.hotStockInfo == null)
                Managers.Manager.hotStockInfo = new DataCenter.Models.StockInfo();

            var t = stockInfo.GetType();
            var ps = t.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            foreach (var p in ps)
            {
                var setP = p.GetSetMethod(true);
                if (setP != null)
                {
                    object o = p.GetGetMethod().Invoke(stockInfo, null);
                    setP.Invoke(Managers.Manager.hotStockInfo, new object[] { o });
                }
            }
        }

        public async static void GetConstituentStocks(DataCenter.Models.IInfo info)
        {
            Panels.GGRankingPanel p;
            if (info.code == "000300")
                p = new Panels.GGRankingPanel("hs300", info.name);
            else
                p = new Panels.GGRankingPanel("zhishu_" + info.code, info.name);
            await p.Init();
            Managers.Manager.panelM.AddPanel(p);
        }

        public static void AskTopUp()
        {
            var dr = MessageBox.Show("订阅用户才能使用该功能，请先订阅服务。\r\n\r\n是否立即订阅？", "需要订阅", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
            if(dr==DialogResult.Yes)
                new Forms.TopUpForm(DataCenter.DataCenter.user.userId, false).ShowDialog();
        }
    }
}
